<?php

//
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;

class NguoiDung extends Eloquent implements UserInterface, RemindableInterface {

    protected $table = 'nguoi_dung';
    public $rules = array(
        'username' => 'required|min:3|max:50',
        'password' => 'required|min:3|max:100'
    );
    public $ruleschangepassword = array(
        'mat_khau' => 'required|min:3|max:100',
        'mat_khau_moi' => 'required|min:3|max:100',
        'xn_mat_khau_moi' => 'required|min:3|max:100'
    );
    protected $hidden = array('password');

    public function validate($data) {
        return Validator::make($data, $this->rules);
    }

    public function validatechangepassword($data) {
        return Validator::make($data, $this->ruleschangepassword);
    }

    public function Authenticate($data) {
        $validation = $this->validate($data);
        //
        if ($validation->passes()) {
            $user = DB::table($this->table)
                    ->select('id', 'username', 'password')
                    ->where('username', $data['username'])
                    ->first();
            if ($user != null) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    public function changePassword($newpassword) {
        $passw = Hash::make($newpassword);
        $userid = Auth::user()->id;
        $data = array('password' => $passw);
        DB::table($this->table)
                ->where('id', $userid)
                ->update($data);
    }

    public function getAuthIdentifier() {
        return $this->getKey();
    }

    public function getAuthPassword() {
        return $this->password;
    }

    public function getRememberToken() {
        return $this->remember_token;
    }

    public function setRememberToken($value) {
        $this->remember_token = $value;
    }

    public function getRememberTokenName() {
        return 'remember_token';
    }

    public function getReminderEmail() {
        return $this->email;
    }

}
